interface IBDlocationScope extends ng.IScope {
    /**
     * 定位按钮
     */
    locaBtn: string;
    /**
     * 车辆id
     */
    carId: string;
    /**
     * 是否为车辆定位
     */
    isVehicle: boolean;
    /**
     * 开通定位Dis
     */
    dredgeDis: boolean;
    /**
     * 定位dis
     */
    locaDis: boolean;
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 定位日期
     */
    creationTime
    /**
     * 司机姓名
     */
    driverName: string;
    /**
     * 定位情况
     */
    statusCode: string;
    /**
     * 手机号码
     */
    phoneNumber: string;

    /**
     * 位置信息
     */
    areaInfo: string;

    
    /**
     * locBtn是否开启
     */
    isdisabled: boolean;
    /**
     * 点击定位事件，必须是已开通
     */
    getlocation: (carId: string, isVehicle) => void;
    /**
     * 开通注册功能，必须是已开通
     */
    dredge: (carId: string, isVehicle: boolean) => void;
    //点击关闭按钮，消除所有地图标点和定位数据
    deletePoint: () => void;


}

// 01609090211 
function tmsBdlocation(locationService: ILocationService): ng.IDirective {
    return {

        templateUrl: './src/directives/tms-BDLocation/tms-bdlocation.html',
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
            carId: "=",
            carCode: "=",
            creationTime:"=",
            driverName: "=",
            statusCode:"=",
            phoneNumber: "=",
            areaInfo:"=",

            back: "&"
        },
        link: ($scope: IBDlocationScope) => {

            $scope.locaBtn = "定位";
            let carId: string = $scope.carId;
            let isVehicle: boolean = $scope.isVehicle = true;

           
            let isdisabled: boolean = $scope.isdisabled = false;
            let creationTime: string = $scope.creationTime = "";

            let areaInfo: string = $scope.areaInfo;

            function initMap(ishide?: boolean) {
                createMap(ishide); //创建地图
                setMapEvent(); //设置地图事件
                addMapControl(); //向地图添加控件
            }

            //创建地图函数：

            let map;
            let hasMap: boolean;
            

            
            
            hasMap = false;

            $scope.$watch('carCode',(newVal,oldVal)=>{
                if(newVal){
                    setTimeout(()=>{
                        initMap();
                    },200);
                }
            })


            function createMap(ishide?: boolean) {
               
               
                map = new BMap.Map("BDMapContent"); //在百度地图容器中创建一个地图
                let point = new BMap.Point(119.446448, 32.164418); //定义一个中心点坐标
                map.centerAndZoom(point, 7); //设定地图的中心点和坐标并将地图显示在地图容器中
                (<any>window).map = map; //将map变量存储在全局
            }

            //地图事件设置函数：
            function setMapEvent() {
                map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
            }

            //地图控件添加函数：
            function addMapControl() {
                //向地图中添加缩放控件
                let ctrl_nav = new BMap.NavigationControl({
                    anchor: BMAP_ANCHOR_TOP_LEFT,
                    type: BMAP_NAVIGATION_CONTROL_LARGE
                });
                map.addControl(ctrl_nav);
                //向地图中添加比例尺控件
                let ctrl_sca = new BMap.ScaleControl({
                    anchor: BMAP_ANCHOR_BOTTOM_LEFT
                });
                map.addControl(ctrl_sca);
            }

            //创建InfoWindow
            // let markerArr: any[];
            // function createInfoWindow(i) {
            //     let json = markerArr[i];
            //     let iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
            //     return iw;
            // }

            // 编写创建标注函数
            function addMarker(point, label) {
                let marker = new BMap.Marker(point);
                map.addOverlay(marker);
                marker.setLabel(label);
            }


            //编写创建标注函数-当前定位点
            function addMarker2(point, label) {
                var myIcon = new BMap.Icon("/src/img/hc.png", new BMap.Size(59, -119));
                var marker2 = new BMap.Marker(point, { icon: myIcon });  // 创建标注
                map.addOverlay(marker2);              // 将标注添加到地图中
                marker2.setLabel(label);
                //marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                map.centerAndZoom(point, 11);
            }


            //逆地址解析函数,起始点使用
            function s_e(adds) {
                myGeo.getPoint(adds, function (point) {
                    if (point) {
                        let address = new BMap.Point(point.lng, point.lat);
                        addMarker(address, new BMap.Label(adds, {
                            offset: new BMap.Size(20, -10)
                        }));
                    }
                });
            }


            //点击定位事件,必须是已开通
            $scope.getlocation = function (CarNo) {
                $scope.locaBtn = "定位中，请稍等...";
                $scope.isdisabled = true;
                $scope.creationTime = "";
                $scope.statusCode = "";
                $scope.areaInfo = "";
                initMap(false);
                //起始点名称数组
                // for (var i = 0; i < adds.length; ++i) {
                //     s_e(adds[i]);
                // }

                locationService.getBDNPLocation(CarNo).then(
                    (resq)=> {
                       //console.log(resq);
                       $scope.areaInfo = resq.data.adr;
                       if (resq.data.utc != null) {
                           $scope.creationTime = resq.data.utc;
                       } else {
                           $scope.creationTime = new Date().getTime();
                       }
                       
                       $scope.statusCode = "定位成功";
                       $scope.isdisabled = false;
                       // 百度API
                       var p0 = parseFloat(resq.data.lon) / 600000;//经度
                       var p1 = parseFloat(resq.data.lat) / 600000;//纬度
                       var pt = new BMap.Point(p0, p1);
                       addMarker2(pt, new BMap.Label($scope.areaInfo, { offset: new BMap.Size(39, -19) })); //offset: new BMap.Size(39, -19)
                       $scope.locaBtn = "定位";
                      
                   },
   
                    (resl) =>{
                       //console.log(resl);
                       $scope.statusCode = "定位失败";
                       $scope.locaBtn = "定位";
                       
                       $scope.isdisabled = false;
                   }
               );
              

            }

           

            




            let myGeo = new BMap.Geocoder();

            


        }

    }
}


tmsBdlocation['$inject'] = ["locationService"]
angular.module('tmsBdlocation', []).directive('tmsBdlocation', tmsBdlocation);